import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.StringRequestEntity
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.methods.PostMethod
import org.apache.commons.httpclient.methods.PutMethod
import javax.swing.JOptionPane

class FireTestController {
     // these will be injected by Griffon
    def model
    def view
    
    def formatter
	
	def doSubmit = {			
		
		def methodType = view.methodType.selectedItem
    	
    	if( methodType == 'GET' ){
    		doGet()
    	}else if( methodType == 'POST' ){
    		doPost()
    	}else if( methodType == 'PUT' ){
    		doPost()
    	} 
	}
	
	
	def doGet = {
	    def url = view.urlTxt.text	
		def statusCode = -1;
		def responseBody = "";
		view.resTxt.text = "Loading ..."
		doOutside{
			try{
				def client = new HttpClient()	
				def getMethod = new GetMethod(url)
				statusCode = client.executeMethod(getMethod)
				responseBody = formatter.format (getMethod.getResponseBodyAsString())		
				
			}
			catch (Exception e)
			{
				doLater{
					view.logTxt.text = view.logTxt.text +"\n"+new Date()+":\n"+ "Exception occured while GET operation :\n" + e.getMessage()
					responseBody = "Operation Failed"
				}
			}
			finally{
				doLater{
					if(statusCode > 0)
					{
					   
					   
					   view.logTxt.text = view.logTxt.text +"\n"+new Date()+":\n"+"Status -" + String.valueOf(statusCode)
					}
					
					
					view.resTxt.text = responseBody
				}
			}
		
		}
	}
	
	def doPost = {
	    def url = view.urlTxt.text	
		def statusCode = -1;
		def responseBody = "";
		def data = view.reqTxt.text		
		view.resTxt.text = "Loading ..."

		doOutside{
			try{
				def client = new HttpClient()	
				def postMethod = new PostMethod(url)
				def reqentity = new StringRequestEntity(data, "application/soap+xml", null);
				postMethod.setRequestEntity(reqentity)
				statusCode = client.executeMethod(postMethod)				
				responseBody = postMethod.getResponseBodyAsString()
			}
			catch (Exception e)
			{
				doLater{
					view.logTxt.text = view.logTxt.text +"\n"+new Date()+":\n"+ "Exception occured while POST operation :\n" + e.getMessage()
					responseBody = "Operation Failed"
				}
			}
			finally{
				doLater{
				
					if(statusCode > 0)
					{
						view.logTxt.text = view.logTxt.text +"\n"+new Date()+":\n"+"Status -" + String.valueOf(statusCode)
					}
					view.resTxt.text = responseBody
				}
			}
		
		}
	}
	
	
	def doPut = {
	    def url = view.urlTxt.text	
		def statusCode = -1;
		def responseBody = "";
		def data = view.reqTxt.text
		if(data == null || data.equals("") )
		{
			JOptionPane.showMessageDialog(app.appFrames[0], "Please provide data for PUT",
                   "Alert", JOptionPane.INFORMATION_MESSAGE)
				   
			return	   

		}
		view.resTxt.text = "Loading ..."

		doOutside{
			try{
				def client = new HttpClient()	
				def putMethod = new PutMethod(url)
				def reqentity = new StringRequestEntity(data, "application/soap+xml", null);
				putMethod.setRequestEntity(reqentity)
				statusCode = client.executeMethod(putMethod)				
				responseBody = putMethod.getResponseBodyAsString()
			}
			catch (Exception e)
			{
				doLater{
					view.logTxt.text = view.logTxt.text +"\n"+new Date()+":\n"+ "Exception occured while POST operation :\n" + e.getMessage()
					responseBody = "Operation Failed"
				}
			}
			finally{
				doLater{
				
					if(statusCode > 0)
					{
						view.logTxt.text = view.logTxt.text +"\n"+new Date()+":\n"+"Status -" + String.valueOf(statusCode)
					}
					view.resTxt.text = responseBody
				}
			}
		
		}
	}
}